namespace ghost {
	/**游戏模块类，相当游戏顶级容器*/
	export class Module extends ModuleBase{		
		public backShape:GYSprite;
		public sceneLay:GYSprite;		
		public floorLay:GYSprite;
		public roleLay:GYSprite;
		public roleTopLay:GYSprite;
		public uiLay:GYSprite;
		// public testLay:GYSprite;
		public gameSp:GYSprite;
		public loseUI:LoseUI;
		public winUI:WinUI;		
		public exitBtn:GYSprite;
		public sceneMgr:SceneManager;
		public goldIcon:GYSprite;
		public goldCount:Laya.Text;
		public curLevelIndex:number;
		public barrierUI:BarrierUI;		
		public callBtn:GYSprite;	
		public callUI:CallUI;	
		public dialogUI:DialogUI;
		
		private _defaultConfig:any;
		
		public startTime:number;
		
		public myPlayer:MyPlayer;	
		private _curScene:SceneBase;
		private _loginSceneType:number;
		private _loginSceneID:string;
		private _config:any;
		private _timeId:number;
		// public warnUI:WarnUI;
		public layerMgr:LayerManager;
		/**平台用户名*/public userName:string;	
		private static _intance:Module;
		
		public static getInstance():Module
		{
			if(Module._intance == null)
				new Module;
			return Module._intance;
		}
		public static crossInfo:any = {
				crossUserName:""
			};
		public constructor() {
			super();			
			Module._intance = this;
		}
		public modulePreStart():void
		{
			super.modulePreStart();
			let s = this;						
			s.startTime = Date.now();									
			
			s.gameSp = new GYSprite;
			s.gameSp.width = s.width;
			s.gameSp.height = s.height;
			s.addChild(s.gameSp);
			s.sceneLay = new GYSprite;
			s.sceneLay.mouseEnabled = true;						
			s.gameSp.addChild(s.sceneLay);
			s.floorLay = new GYSprite;
			s.floorLay.mouseThrough = true;			
			s.gameSp.addChild(s.floorLay);
			s.roleLay = new GYSprite;
			s.roleLay.mouseEnabled = true;			
			s.gameSp.addChild(s.roleLay);			
			s.roleTopLay = new GYSprite;
			s.roleTopLay.mouseThrough = true;
			s.gameSp.addChild(s.roleTopLay);
			s.uiLay = new GYSprite;
			s.gameSp.addChild(s.uiLay);
			// s.testLay = new GYSprite;
			// s.testLay.mouseEnabled = false;
			// s.addChild(s.testLay);
			s.layerMgr = new LayerManager(s);

			s.skillBoard = new SkillBoard;
			s.skillBoard.y = s.height - s.skillBoard.height;
			s.skillBoard.x = s.width - s.skillBoard.width;
			s.skillBoard.visible = false;
			s.uiLay.addChild(s.skillBoard);			
			s.dirBoard = new DirectorBoard;							
			s.uiLay.addChild(s.dirBoard);						

			s.backShape = new GYSprite;			
			s.addChild(s.backShape);
			

			s.exitBtn = new GYSprite;
			s.exitBtn.texture = Main.instance.getRes(Conf.scene + "exit.png");
			s.exitBtn.x = 20;
			s.exitBtn.y = 20;
			s.exitBtn.on(Laya.Event.CLICK,s,function(e:Laya.Event):void{
				Main.instance.exit();
			});			
			s.addChild(s.exitBtn);	
			s.exitBtn.visible = false;	
			s.goldIcon = new GYSprite;
			s.goldIcon.texture = Main.instance.getRes(Conf.scene + "gold.png");			
			s.addChild(s.goldIcon)
			s.goldCount = new Laya.Text;
			s.goldCount.width = 200;
			s.goldCount.fontSize = 30;
			s.goldCount.stroke = 4;
			s.goldCount.strokeColor = "#000000";
			s.goldCount.color = "#ffffff";
			s.goldCount.text = "0";
			s.addChild(s.goldCount);
			
			s.callBtn = new GYSprite;
			s.callBtn.texture = Main.instance.getRes(Conf.scene + "commonBtn.png");					
			s.callBtn.on(Laya.Event.CLICK, s, s.openTower);
			s.callBtn.x = 360 - 114;
			let txt = new Laya.Text;
			txt.width = s.callBtn.width;
			txt.align = "center";
			txt.stroke = 4;
			txt.strokeColor = "#000000"
			txt.color = "#ffffff";
			txt.text = "召唤"
			txt.fontSize = 30;
			txt.valign = "middle";
			txt.height = s.callBtn.height;
			txt.x = 4;
			txt.y = 5;
			s.callBtn.addChild(txt);				
			s.addChild(s.callBtn);

			s.sceneMgr = new SceneManager(s.gameSp,s.sceneLay,s.gameSp.width,s.gameSp.height,s);
			
			GameDispatcher.instance.on(GameEvent.GOLD_CHANGE, s, s.goldChanged);

			PublicVar.init();
			SkinDictionary.init();
			s.initBeforeLogin();
					
		}
		private goldChanged():void{
			let s = this;
			s.goldCount.text = MyPlayerData.getInstance().gold+"";
		}
		private openTower():void
		{
			let s = this;
			if(!(<CommonScene>s.sceneMgr.sceneLogic).isBedValid())
				return;
			if(s.callUI == null)
				s.callUI = new CallUI
			s.callUI.show(this);
		}
		protected getDefaultConfig():any
		{
			let s= this;
			if(s._defaultConfig == null){
				s._defaultConfig = {
					scene:SceneConfig.getSceneConfig("B")
				};
			};
			return s._defaultConfig;
		}
		protected start():void
		{
			super.start();
			let s = this;
			SoundManager.instance.closeBGM();
			s.runGame();			
			
		}
		private runGame():void
		{
			let s= this;
			s.loginRet({sceneID:Main.levelKey,sceneType:2});			
		}		
		
		private gameSpResize(e:Laya.Event=null):void
		{
			let s = this;			
			
		}
		/**处理模块适配*/
		public resize(w:number,h:number):void
		{
			let s = this;
			super.resize(w,h);	
			s.resizeUI();
			s.gameSp.x = 0;
			s.gameSp.y = s._frameBound.y;
			s.gameSp.width = s.width;
			s.gameSp.height = s._frameBound.height;
			s.sceneMgr.setSceneSize(s.gameSp.width,s.gameSp.height);
			s.sceneLay.scrollRect = new Laya.Rectangle(0,0,s.width,s.frameBound.height);
			// s.testLay.graphics.clear();
			// s.testLay.graphics.drawRect(s.frameBound.x,s.frameBound.y,s.frameBound.width,s.frameBound.height,"#fff000");
			// s.testLay.graphics.drawRect(0,0,s.width,s.height,"#ff0000");
			// s.testLay.alpha = 0.5;
			let len:number;
			let display:GameUIBase;
			len = s.numChildren;
			while(--len>-1)
			{
				display = <GameUIBase>s.getChildAt(len);
				if("resize" in display)
				{
					// display.x = s.frameBound.x;
					// display.y = s.frameBound.y;
					// display.width = s.frameBound.width;
					// display.height = s.frameBound.height;
					(<any>display).resize();
				}
					
			}
		}	
		/**处理UI适配*/
		private resizeUI():void
		{
			let s = this;
			let offsetLeft:number, offsetTop:number;			
			
			offsetLeft = s._frameBound.x;
			offsetTop = s._frameBound.y;
			if(s.barrierUI)s.barrierUI.x = offsetLeft;
			
			s.exitBtn.x = 30;
			s.exitBtn.y = 30 + offsetTop;
			s.goldIcon.x = s.exitBtn.x + s.exitBtn.width + 40;
			s.goldIcon.y = s.exitBtn.y + (s.exitBtn.height - s.goldIcon.height >> 1);
			s.goldCount.x = s.goldIcon.x + s.goldIcon.width + 10;
			s.goldCount.y = s.goldIcon.y + (s.goldIcon.height - s.goldCount.height >> 1) + 2;
			s.callBtn.x = GameManager.DESIGN_WIDTH - s.callBtn.width >> 1;
			s.callBtn.y = s._frameBound.y + s._frameBound.height - 30 - s.callBtn.height;

			s.dirBoard.y = s._frameBound.y + s._frameBound.height - s.dirBoard.height;
			// s.dirBoard.x = s._frameBound.x;
		}	
		public show(pr:GYSprite=null):void
		{let s = this;
			super.show(pr);			
			CommonUtil.addStageLoop(s.loop,s);		
			s.resize(GameManager.STAGE_WIDTH, GameManager.STAGE_HEIGHT);
			
			if(GameManager.debug)
			{
				s.barrierUI = new BarrierUI(s.sceneMgr);
				s.barrierUI.show(s.gameSp);
			}			
			s.dirBoard.visible = !GameManager.debug;
			
		}				
		
		public hide():void
		{let s = this;
			super.hide();			
			CommonUtil.delStageLoop(s.loop,s);			
		}
		private loop(t:number):void
		{
			let s = this;
			s.sceneMgr.loop(t);
			let skills:any[] = Main.gameConfig.skills;
			let min:number = Number.MAX_VALUE;
			let i:number,len:number;
			len = skills.length;
			for(i=0;i<len;++i)
			{
				min = Math.min(skills[i].cost, min);
			}
			s.callBtn.visible = MyPlayerData.getInstance().gold >= min;
			if(GuideTip.instance.step == 2 && s.callBtn.visible)
				GuideTip.instance.addGuide();
			if(t - Main.gameStartTime > Main.levelPassTime)
				s.exitBtn.visible = true;
		}
		private initBeforeLogin():void
		{
			let s = this;
			PoolUtil.registerClassInfo(map2D.ANode,"ANode",["IPoolObject"]);
			PoolUtil.registerClassInfo(TargetData,"TargetData",["IPoolObject"]);			
			PoolUtil.setPoolLengthWarn(map2D.ANode, 2000);
		}
		/**收到登录回复*/
		private loginRet(data:any):void
		{
			let s = this;
			s._loginSceneID = data.sceneID;
			s._loginSceneType = data.sceneType;			
			s.loginedInit();
			s.initGame();			
		}
		private loginedInit():void
		{
			let s =this;		
			s.intoScene(new CommonScene(s.sceneMgr),2,s._loginSceneID);
		}
		/**请求进入场景*/
		public intoScene(sceneObject:SceneBase,sceneType:number, sceneID:string, difficulty:number=0, inType:number=1):void
		{
			let s= this;
			if(s._curScene)
				s._curScene.dispose();
			s._curScene = sceneObject;		
			s.sceneMgr.setSceneLogic(s._curScene);
			s._config = {scene:SceneConfig.getSceneConfig(sceneID)};
			s.intoSceneRet(null);
		}
		/**收到进入场景回复*/
		private intoSceneRet(data:any):void
		{
			let s= this;											
			s.sceneMgr.loadScene(s._config.scene);			
				
		}
		
		public enterNextScene():boolean
		{
			++Main.curLevelIndex;
			if(Main.curLevelIndex < SceneConfig.levels.length)
			{
				Main.levelKey = SceneConfig.levels[Main.curLevelIndex];
				let obj:any = SceneConfig.getSceneConfig(Main.levelKey);
				if(obj == null)
				{
					let s= this;
					Main.instance.myLoader.loadData("sceneConfig/" + Main.levelKey + ".json",s.loadConfigComp,s,null,GYLoader.TYPE_TEXT);        
				}
				return true;
			}			
			return false;
		}
		private loadConfigComp(l:LoadInfo):void
		{
			if(l.content == null || l.content.res == null)
			{            
				console.error("加载配置失败!" + l.path);
				return;
			}			
			let s= this;
			let resArr:string[] = [];        
			s._config = JSON.parse(l.content.res);
			resArr = s._config.res;
			if(resArr == null)
				resArr = SceneConfig.defaultRes;
			else if(resArr.length == 0)
				resArr = resArr.concat(SceneConfig.defaultRes);
			SceneConfig.setSceneConfig(Main.levelKey, s._config.sceneConfig);
			s.sceneMgr.loadScene(s._config.sceneConfig);			
		}
		private initGame():void
		{			
					
		}		
				
		public gameEnd(win:boolean):void
		{
			let s= this;
			Main.result = win?1:0;
			if(win)
			{
				if(s.winUI == null)
					s.winUI = new WinUI;
				s.winUI.show(this);
			}
			else
			{
				if(s.loseUI == null)
					s.loseUI = new LoseUI;
				s.loseUI.show(this);
			}
		}
		public dispose(disposeChild: boolean = true, removeChild: boolean = true, forceDispose: boolean = false):void
		{
			super.dispose(disposeChild, removeChild, forceDispose);
			this.sceneMgr.dispose();
			let s = this;
			GameDispatcher.instance.off(GameEvent.GOLD_CHANGE, s, s.goldChanged);
		}
		public dirBoard:DirectorBoard;
		public skillBoard:SkillBoard;		
	}
}